package com.shopping.controller.servlet;


import com.shopping.entity.Book;
import com.shopping.entity.PageBean;
import com.shopping.service.ClientService;
import com.shopping.service.ManagerService;
import com.shopping.service.impl.ClientServiceImpl;
import com.shopping.service.impl.ManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/index")
public class indexServlet extends HttpServlet {
    private ClientService service = new ClientServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String pindex=request.getParameter("pindex");
        String type="all";
        //分页查找该分类下的产品
        PageBean<Book> pageBean =null;
        if(pindex!=null) {
            int tindex=Integer.parseInt(pindex);
            if(tindex>0) {
                pageBean=service.findBooktype(type,tindex,12);
            }else {
                pageBean=service.findBooktype(type,1,12);
            }
        }else {
            pageBean=service.findBooktype(type,1,12);
        }
        request.setAttribute("pageBean", pageBean);
        request.setAttribute("type", type);
        request.setAttribute("op", "book");
       // request.getRequestDispatcher("/showBook.jsp").forward(request, response);


//        List<Book> books=service.findBookByTime();
//        request.setAttribute("books",books);
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                + path + "/";
//		    resp.setHeader("refresh", "0;URL=http://localhost:8080/BookShopping/index");
        request.getRequestDispatcher("/showBook.jsp").forward(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        doGet(request,response);
    }
}
